package com.cjack.frenchexcel.dao;

import com.cjack.frenchexcel.table.AdminUserTable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * 后台账户管理
 * Created by root on 10/5/19
 */
public interface UserAdminDao extends JpaRepository<AdminUserTable, Long> {

    List< AdminUserTable> findByNomAndPrenomStartingWith(String nom, String prenom);

    @Transactional( timeout = 100)
    @Modifying
    @Query( value = "insert into fe_adminuser( id,create_time,last_login_time,password,phone,head_img,email,collaborateur,job_number,nom,nom_number,prenom,role_id) values(:#{#data.id},:#{#data.createTime},:#{#data.lastLoginTime},:#{#data.password},:#{#data.phone},:#{#data.headImg},:#{#data.email},:#{#data.collaborateur},:#{#data.jobNumber},:#{#data.nom},:#{#data.nomNumber},:#{#data.number},:#{#data.prenom},:#{#data.role});", nativeQuery = true)
    void restoreData( @Param("data") AdminUserTable table);

    /**
     * 查询除了超级管理员之外的所有员工数据,超级管理员的roleId是 1,
     * @param superAdminCode
     * @return
     */
    @Query( value = " select * from fe_adminuser where role_id not in( select id from fe_role where code=?1) order by job_number asc;", nativeQuery = true)
    List< AdminUserTable> findByBesideCodeOrderByJobNumberAsc( String superAdminCode);

    /**
     * 查询制定角色的用户列表，按照job从小到大排序
     * @param
     * @return
     */
    @Query( value = " select * from fe_adminuser where role_id in( select id from fe_role where code=?1) order by job_number desc;", nativeQuery = true)
    List< AdminUserTable> findByRoleCode( String roleCode);

    /**
     * 清除除了超级管理员之外的所有数据
     * @param code
     */
    @Transactional( timeout = 100)
    @Modifying
    @Query( value = " delete from fe_adminuser where role_id not in( select id from fe_role where code=?1);", nativeQuery = true)
    void clearAll( String code);

    public AdminUserTable findByJobNumber( String jobNumber);

    public List<AdminUserTable> findByJobNumberLike( String jobNumber);

    public AdminUserTable findByUserProfileId( String userProfileId);
}
